package com.xydtech.common.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.exception.ApiException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: karl-api
 * @description: 封装json对象，所有返回结果都使用它
 * @Author: Karl
 * @QQ: 1398080880
 * @WX: 825541551
 * @PersonalBelief: 没有最好，只有更好，努力创造美好未来。
 * @Date: Create in 2019-11-04 08:43:41
 **/
@Slf4j
@ApiModel(value = "返回结果【统一数据结构】")
public class ResponseModel {

    /**
     * 返回码
     */
    @ApiModelProperty(value = "返回状态码：【0成功，其他均为不成功】")
    private int code;

    /**
     * 返回结果描述
     */
    @ApiModelProperty(value = "返回信息【结果提示信息】")
    private String msg;

    /**
     * 返回内容
     */
    @ApiModelProperty(value = "返回object封装对象【json数据】")
    private Object data;

    /**
     * 返回总条数
     */
    @ApiModelProperty(value = "总条数")
    private Long total;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public String getMsg() {
        return msg;
    }

    public Object getData() {
        return data;
    }

    public Long getTotal() {
        return total;
    }

    public ResponseModel() {
    }

    public ResponseModel(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = "";
    }

    public ResponseModel(int code, String msg, Object content) {
        this.code = code;
        this.msg = msg;
        this.data = content;
    }

    public ResponseModel(int code, String msg, Long total, Object content) {
        this.code = code;
        this.msg = msg;
        this.data = content;
        this.total = total;
    }

    public ResponseModel(ResultStatus status) {
        this.code = status.getCode();
        this.msg = status.getMsg();
        this.data = "";
    }

    public ResponseModel(Exception ex) {
        this.code = ResultStatus.FAIL.getCode();
        this.msg = ex.getMessage();
        this.data = "";
    }

    public ResponseModel(ResultStatus status, Object content) {
        this.code = status.getCode();
        this.msg = status.getMsg();
        this.data = content;
    }

    public void setData(Object content) {
        this.data = content;
    }

    public ResponseModel(ResultStatus status, Long total, Object content) {
        this.code = status.getCode();
        this.msg = status.getMsg();
        this.data = content;
        this.total = total;
    }

    public static ResponseModel ok(Object content) {
        return new ResponseModel(ResultStatus.SUCCESS, content);
    }


    public static ResponseModel okToJSON(Object content) {
        JSONObject json = (JSONObject) JSON.toJSON(content);
        return new ResponseModel(ResultStatus.SUCCESS, json);
    }
    public static ResponseModel okToJSONArray(Object content) {
        JSONArray array= JSONArray.parseArray(JSON.toJSONString(content));
        return new ResponseModel(ResultStatus.SUCCESS, array);
    }

    public static ResponseModel okToJSONArray(Long total, Object content) {
        JSONArray array= JSONArray.parseArray(JSON.toJSONString(content));
        return new ResponseModel(ResultStatus.SUCCESS, total,array);
    }


    public static ResponseModel ok() {
        return new ResponseModel(ResultStatus.SUCCESS);
    }

    public static ResponseModel error(ResultStatus error) {
        return new ResponseModel(error);
    }

    public static ResponseModel error(ResultStatus error, Object content) {
        return new ResponseModel(error, content);
    }

    public static ResponseModel error(String msg) {
        return new ResponseModel(ResultStatus.FAIL.getCode(), msg);
    }

    public static ResponseModel error(Exception ex, Class clazz, String from) {
        //log.error("在方法" + from + "出错：" + ex.getMessage() + "详细内容：" + ex, clazz);

        ex.printStackTrace();
        return new ResponseModel(ResultStatus.FAIL);
    }

    public static ResponseModel error(ApiException error) {
        if (null != error.getStatus()) {
            return new ResponseModel(error.getStatus());
        }
        return new ResponseModel(ResultStatus.FAIL.getCode(), error.getMessage());
    }

    public static ResponseModel error(Exception ex) {
        return new ResponseModel(ex);
    }

    @Override
    public String toString() {
        return "ResponseModel{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", total=" + total +
                '}';
    }
}
